//获取应用实例
const app = getApp()
import api from '../../utils/api'

Page({
  data: {
    user:{}
  },
  onLoad: function (options) {
    // app.getUserInfo(function (userInfo) {
      //更新数据
      // console.log(userInfo,"09809880809")
      // that.setData({
      //   userInfo: userInfo
      // })
    // })
    
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // let user = wx.getStorageSync("user");
    // this.setData({
    //   user: user
    // })
    this.updUser();
  },
  exit:function(){
    wx.showModal({
      title: '提示',
      content: '确定要退出吗？',
      showCancel: true,//是否显示取消按钮
      cancelText: "否",//默认是“取消”
      confirmText: "是",//默认是“确定”
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          app.globalData.stoves = [];
          app.globalData.cartGoods = [];
          app.globalData.cartHalfGoods = [];
          app.globalData.retireList = [];
          app.globalData.evaluate = {};
          app.globalData.sfpj = 0;
          app.globalData.food = {};
          wx.clearStorage({
            success:function(){
              wx.redirectTo({
                url: '../login/login'
              })
            }
          })
        }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  updUser() {
    let _this = this;
    let user = wx.getStorageSync("user");
    if (!user.bh) {
      wx.redirectTo({
        url: '../login/login',
      })
      return;
    }
    let param = { "patientId": user.bh };
    api.POST({
      url: 'login.action',
      params: param,
      success: function (res) {
        let success = res.data.success;
        if (success == "true") {
          let user = res.data.data[0]; //replace(/\s+/g, '')
          user.bh = user.bh.replace(/\s+/g, '');
          _this.setData({
            user: user
          })
          wx.setStorage({
            key: 'user',
            data: user
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
            duration: 5000
          })
          wx.clearStorage({
            success: function () {
              wx.redirectTo({
                url: '../login/login'
              })
            }
          })
        }
      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '请求失败',
          duration: 5000
        })
      },
    })

  },
})